<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/2/1
 * Time: 9:56
 */

namespace app\index\controller;


use app\common\lib\common\Redis;
use app\common\lib\common\StringHandle;


class Login extends Base
{
    public function index()
    {
        $loginData = $this->request->post();
        //获取登录code
        $code = Redis::get(StringHandle::front($loginData['phone_num']));
        if (empty($code)) {
            return $this->ajaxError('验证码已过期');
        }
        if ($code == $loginData['code']) {
            // 组装登录token信息
            $userToken = [
                'user'=>$loginData['phone_num'],
                'token'=>md5($loginData['phone_num'].time()),
                'time'=>time(),
                'isLogin'=>true
            ];
//            登录信息保存到redis
            Redis::set(StringHandle::userFront($loginData['phone_num']),$userToken,StringHandle::time('userLogin'));
            //组装task任务删除登录验证的code task任务数据
            $taskData = $this->taskData('delSmsCode',['tel'=>$loginData['phone_num']]);
            // 创建task任务 并进行投递删除登录验证的code
            $_POST['swoole_http_obj']->task($taskData);
            return $this->ajaxSuccess('登陆成功',1,$userToken);
        }else{
            return $this->ajaxError('验证码错误');
        }
    }
}